/**
 * 
 */
package fr.miage.client.presentation.top;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

import fr.miage.client.metier.client.ClientBusinessService;
import fr.miage.client.metier.client.ClientBusinessServiceAsync;
import fr.miage.client.metier.entite.Client;
import fr.miage.client.metier.entite.Ville;
import fr.miage.client.presentation.LightBoxDialog;

/**
 * @author romain, vincent
 *
 */
public class Inscription {
	ServiceDefTarget endpoint;
	ClientBusinessServiceAsync clientService;
	
	
	
	
	public Inscription() {
	}
	
	
	/**
	 * Initialize this example.
	 */
	
	
	public Widget onInitialize() {
		// Create the dialog box
		
		final LightBoxDialog LightBoxDialog = createLightBoxDialog();
		LightBoxDialog.setAnimationEnabled(true);
		
		// Create a button to show the dialog Box
		Button openButton = new Button("Inscription",
				new ClickListener() {
			public void onClick(Widget sender) {
				LightBoxDialog.center();
				LightBoxDialog.show();
			}
		});		
		openButton.setWidth("180");
		
		// Add the button and list to a panel
		VerticalPanel vPanel = new VerticalPanel();
		vPanel.setSpacing(8);
		vPanel.add(openButton);
		// Return the panel
		return vPanel;
	}
	
	/**
	 * Create the dialog box for this example.
	 * 
	 * @return the new dialog box
	 */
	
	private LightBoxDialog createLightBoxDialog() {
		final LightBoxDialog LightBoxDialog = new LightBoxDialog();
		LightBoxDialog.ensureDebugId("cwLightBoxDialog");
		LightBoxDialog.setText("Fenêtre d'inscription");
		
		// Create a table to layout the content
		VerticalPanel dialogContents = new VerticalPanel();
		dialogContents.setSpacing(4);
		LightBoxDialog.setWidget(dialogContents);
		// Table dans laquelle on place les éléments
		final FlexTable flexTable = new FlexTable();
		dialogContents.add(flexTable);
			

		flexTable.setHTML(0, 0, "Nom : ");
		final TextBox nom = new TextBox();
		flexTable.setWidget(0, 1, nom);
		
		flexTable.setHTML(1, 0, "Prénom : ");
		final TextBox prenom = new TextBox();
		flexTable.setWidget(1, 1, prenom); 
		
		flexTable.setHTML(2, 0, "Adresse : ");
		final TextBox adresse = new TextBox();
		flexTable.setWidget(2, 1, adresse); 
		
		flexTable.setHTML(3, 0, "Code postal : ");
		final TextBox codePostal = new TextBox();
		
		
		final Button rechercherVille = new Button("Rechercher ville");
		rechercherVille.addClickListener(new ClickListener(){
			public void onClick(Widget sender) {
			
					Window.alert("Nouvelle fenetre avec la liste des CP et villes - non fonctionnel");
					
			}		
		});
		
				
		flexTable.setWidget(3, 1, codePostal); 
		flexTable.setWidget(3, 2, rechercherVille); 
		
		
		flexTable.setHTML(4, 0, "Téléphone : ");
		final TextBox telephone = new TextBox();
		flexTable.setWidget(4, 1, telephone); 
		
		flexTable.setHTML(5, 0, "Email : ");
		final TextBox email = new TextBox();
		flexTable.setWidget(5, 1, email); 
		
		flexTable.setHTML(6, 0, "Mot de passe : ");
		final PasswordTextBox motdepasse = new PasswordTextBox();
		flexTable.setWidget(6, 1, motdepasse); 
		
		flexTable.setHTML(7, 0, "Confirmez le mot de passe : ");
		final PasswordTextBox confmotdepasse = new PasswordTextBox();
		flexTable.setWidget(7, 1, confmotdepasse); 
		
		// Bouton de validation
		final Button validateButton = new Button();
		flexTable.setWidget(8, 2, validateButton);
		validateButton.setText("Valider");
		
		
		validateButton.addClickListener(
				new ClickListener() {
					public void onClick(Widget sender) {
						// création du proxy 
									
						clientService = (ClientBusinessServiceAsync) GWT.create(ClientBusinessService.class);
						endpoint = (ServiceDefTarget) clientService;
						String moduleRelativeURL = GWT.getModuleBaseURL() + "client";
						endpoint.setServiceEntryPoint(moduleRelativeURL);
						
						AsyncCallback callback = new AsyncCallback(){
							public void onSuccess(Object result) {  		
								Window.alert("Vous êtes maintenant inscrit - pas encore logué");
								LightBoxDialog.hide();
							}
							
							public void onFailure(Throwable caught) {
								Window.alert("Prob de BD");
								System.out.println("callback inscript fail");
							}
						};
											
						if (nom.getText() == "" || prenom.getText()== "" || adresse.getText()== "" || email.getText()== "" || telephone.getText()== "" || codePostal.getText()== "" || motdepasse.getText()== "" || confmotdepasse.getText()== ""){
							
							Window.alert("Tous les champs doivent être remplis");
						}
						else{		
							
							Client client = new Client();
							
							client.setNom(nom.getText());
					        client.setPrenom(prenom.getText());
					        client.setAdresse(adresse.getText());				        				        
					        client.setEmail(email.getText());				        
					        client.setTelephone(telephone.getText());
					        
					        Ville ville = new Ville(Integer.parseInt(codePostal.getText()));
					        client.setVille(ville);
					        				        
					        clientService.createClient(client,callback); 
						}
					}
				});		
		
		dialogContents.add(validateButton);
		
		// Bouton d'annulation
		final Button anulButton = new Button();
		flexTable.setWidget(9, 2, anulButton);
		anulButton.setText("Annuler");
		
		anulButton.addClickListener(
				new ClickListener() {
					public void onClick(Widget sender) {
						
						LightBoxDialog.hide();
						nom.setText("");
						prenom.setText("");
						adresse.setText("");
						codePostal.setText("");
						telephone.setText("");
						email.setText("");
						motdepasse.setText("");
						confmotdepasse.setText("");
					}
				});	
		
		dialogContents.add(anulButton);
		
		// Return the dialog box
		return LightBoxDialog;
	}
	
	
}
